<?php


namespace App\Http\Controllers\WebApi\Business;


use App\Models\ImConfig;

class ConfigBusiness
{
    //服务器配置
    public static $server = [
        ['title' => '手机号搜索用户', 'key' => 'telephoneSearchUser', 'value' => 1, 'desc' => ''],//0关闭 1精确搜索 2模糊搜索
        ['title' => '昵称搜索用户', 'key' => 'nicknameSearchUser', 'value' => 1, 'desc' => ''],//0关闭 1精确搜索 2模糊搜索
        ['title' => '手机号登陆', 'key' => 'isTelephoneLogin', 'value' => 1, 'desc' => ''],    //0关闭 1开启
        ['title' => '用户ID登陆', 'key' => 'isUserIdLogin', 'value' => 1, 'desc' => ''],//0关闭 1开启
        ['title' => '使用手机号或者用户名注册', 'key' => 'regeditPhoneOrName', 'value' => 0, 'desc' => ''],//0手机号 1用户名
        ['title' => '是否保存接口请求日志', 'key' => 'isSaveRequestLogs', 'value' => 0, 'desc' => ''],//0关闭 1开启
        ['title' => '注册默认成为好友的用户手机号', 'key' => 'defaultTelephones', 'value' => '', 'desc' => '支持填写多个带区号的手机号，多个带区号的手机号之间用“，”分隔'],
        ['title' => '默认漫游时长', 'key' => 'roamingTime', 'value' => 4, 'desc' => ''],//0不漫游 1永久 2一小时 3一天 4一周 5一个月 6一季 7一年
        ['title' => '群人数上限', 'key' => 'max_count', 'value' => 2000, 'desc' => ''],//群人数上限
        ['title' => '群公共私有类型', 'key' => 'open', 'value' => 1, 'desc' => ''],//公共私有类型：1：公开，2：私有
        ['title' => '是否开启群组邀请确认', 'key' => 'is_audit', 'value' => 0, 'desc' => ''],//0关闭 1开启
        ['title' => '群组减员发送通知', 'key' => 'isAttritionNotice', 'value' => 1, 'desc' => ''],//0关闭 1开启
        ['title' => '是否允许显示群成员', 'key' => 'showMember', 'value' => 1, 'desc' => ''],//0关闭 1开启
        ['title' => '是否允许普通成员邀请好友', 'key' => 'is_allow', 'value' => 1, 'desc' => ''],//0关闭 1开启
    ];
    //客户端配置
    public static $client = [
        ['title' => 'XMPP 主机 host', 'key' => 'XMPPHost', 'value' => 'im.sdfshbkj.com', 'desc' => ''],
        ['title' => 'XMPP 虚拟域名', 'key' => 'XMPPDomain', 'value' => 'im.sdfshbkj.com', 'desc' => ''],
        ['title' => '接口URL', 'key' => 'apiUrl', 'value' => 'http://imapi.sdfshbkj.com/', 'desc' => ''],
        ['title' => '头像下载URL', 'key' => 'downloadAvatarUrl', 'value' => 'http://head.sdfshbkj.com/', 'desc' => ''],
        ['title' => '资源下载URL', 'key' => 'downloadUrl', 'value' => 'http://file.sdfshbkj.com/', 'desc' => ''],
        ['title' => '资源上传URL', 'key' => 'uploadUrl', 'value' => 'http://upload.sdfshbkj.com/', 'desc' => ''],
        ['title' => '视频服务器URL', 'key' => 'jitsiServer', 'value' => 'https://syee.fjfwzb.com/', 'desc' => ''],
        ['title' => '直播服务器URL', 'key' => 'liveUrl', 'value' => 'rtmp://v1.one-tv.com:1935/live/', 'desc' => ''],
        ['title' => 'IOS应用AppleId', 'key' => 'appleId', 'value' => '', 'desc' => ''],
        ['title' => '公司下载页网址', 'key' => 'website', 'value' => '', 'desc' => '公司下载页网址,用于生成二维码链接'],
        ['title' => '推广标题', 'key' => 'extensionTitle', 'value' => '', 'desc' => '发现模块推广标题'],
        ['title' => '推广网址', 'key' => 'extensionUrl', 'value' => '', 'desc' => '发现模块推广链接'],//发现模块推广链接
        ['title' => '是否开放IOS零钱', 'key' => 'displayRedPacket', 'value' => 0, 'desc' => ''],//0关闭 1开启
        ['title' => '是否启用已读消息回执', 'key' => 'isOpenReadReceipt', 'value' => 1, 'desc' => ''],//0关闭 1开启
        ['title' => '启用手机联系人', 'key' => 'showContactsUser', 'value' => 1, 'desc' => ''],//0关闭 1开启
        ['title' => '是否开启注册', 'key' => 'isOpenRegister', 'value' => 1, 'desc' => ''],//0关闭 1开启
        ['title' => '是否开启好友搜索功能', 'key' => 'hideSearchByFriends', 'value' => 1, 'desc' => ''],//0关闭 1开启
        ['title' => '是否开启普通用户搜索好友', 'key' => 'isCommonFindFriends', 'value' => 1, 'desc' => ''],//0关闭 1开启
        ['title' => '是否开启普通用户创建群组', 'key' => 'isCommonCreateGroup', 'value' => 1, 'desc' => ''],//0关闭 1开启
        ['title' => '是否开启位置相关服务', 'key' => 'isOpenPositionService', 'value' => 1, 'desc' => ''],//0关闭 1开启
        ['title' => '公司名称', 'key' => 'companyName', 'value' => '', 'desc' => ''],
        ['title' => '版权信息', 'key' => 'copyright', 'value' => '', 'desc' => ''],
        ['title' => 'Android最新版本号', 'key' => 'androidVersion', 'value' => '', 'desc' => ''],
        ['title' => 'Android最新下载URL', 'key' => 'androidAppUrl', 'value' => '', 'desc' => ''],
        ['title' => 'Android更新说明', 'key' => 'androidExplain', 'value' => '', 'desc' => ''],
        ['title' => 'IOS最新版本号', 'key' => 'iosVersion', 'value' => '', 'desc' => ''],
        ['title' => 'IOS最新下载URL', 'key' => 'iosAppUrl', 'value' => '', 'desc' => ''],
        ['title' => 'IOS更新说明', 'key' => 'iosExplain', 'value' => '', 'desc' => ''],
        ['title' => 'PC最新版本号', 'key' => 'pcVersion', 'value' => '', 'desc' => ''],
        ['title' => 'PC最新下载URL', 'key' => 'pcAppUrl', 'value' => '', 'desc' => ''],
        ['title' => 'PC更新说明', 'key' => 'pcExplain', 'value' => '', 'desc' => ''],
        ['title' => 'MAC最新版本号', 'key' => 'macVersion', 'value' => '', 'desc' => ''],
        ['title' => 'MAC最新下载URL', 'key' => 'macAppUrl', 'value' => '', 'desc' => ''],
        ['title' => 'MAC更新说明', 'key' => 'macExplain', 'value' => '', 'desc' => ''],
        ['title' => 'Android禁用版本号', 'key' => 'androidDisable', 'value' => '', 'desc' => '凡低于此版本号的禁用
示例：5.1.0'],
        ['title' => 'IOS禁用版本号', 'key' => 'iosDisable', 'value' => '', 'desc' => '凡低于此版本号的禁用
示例：5.1.0'],
        ['title' => 'PC禁用版本号', 'key' => 'pcDisable', 'value' => '', 'desc' => '凡低于此版本号的禁用
示例：5.1.0'],
        ['title' => 'Mac禁用版本号', 'key' => 'macDisable', 'value' => '', 'desc' => '凡低于此版本号的禁用
示例：5.1.0'],
    ];

    /**
     * 初始化服务端配置
     * @return bool
     */
    public static function initServer()
    {
        $count = ImConfig::where('type', ImConfig::SERVER_TYPE)->count();
        if (!$count) {
            return ImConfig::insert(self::paramProcess(self::$server, ImConfig::SERVER_TYPE));
        }
        return false;
    }

    /**
     * 初始化客户端配置
     * @return bool
     */
    public static function initClient()
    {
        $count = ImConfig::where('type', ImConfig::CLIENT_TYPE)->count();
        if (!$count) {
            return ImConfig::insert(self::paramProcess(self::$client, ImConfig::CLIENT_TYPE));
        }
        return false;
    }

    /**
     * 获取服务端所有配置
     * @return mixed
     */
    public static function getServer()
    {
        return ImConfig::where('type', ImConfig::SERVER_TYPE)->select(['title', 'key', 'value', 'desc'])->get();
    }

    /**
     * 获取客户端所有配置
     * @return mixed
     */
    public static function getClient()
    {
        return ImConfig::where('type', ImConfig::CLIENT_TYPE)->select(['title', 'key', 'value', 'desc'])->get();
    }

    /**
     * 根据key获取服务端配置
     * @param array $keys
     * @return array
     */
    public static function getServerConfigs($keys = [])
    {
        if (!empty($keys)) {
            return ImConfig::where('type', ImConfig::SERVER_TYPE)->whereIn('key', $keys)
                ->select(['title', 'key', 'value', 'desc'])->get();
        }
        return [];
    }

    /**
     * 根据key获取客户端配置
     * @param array $keys
     * @return array
     */
    public static function getClientConfigs($keys = [])
    {
        if (!empty($keys)) {
            return ImConfig::where('type', ImConfig::CLIENT_TYPE)->whereIn('key', $keys)
                ->select(['title', 'key', 'value', 'desc'])->get();
        }
        return [];
    }

    /**
     * 获取服务端群组配置
     * @return array
     */
    public static function getServerGroupConfigs()
    {
        $keys = [
            'max_count',//群人数上限
            'open',//公共私有类型：1：公开，2：私有
            'is_audit',//是否开启群组邀请确认：0：不需要，1：需要
            'is_allow',//是否允许普通成员邀请好友
            'isAttritionNotice',//群组减员发送通知
            'showMember',//是否允许显示群成员
        ];
        $data = self::getServerConfigs($keys);
        self::valueProcess($data, $res);
        return $res;
    }

    /**
     * @param $key 获取第三方配置项
     * @return mixed
     * @author an
     * @time 2021-12-06 16:19
     */
    public static function thirdPartyFirst($key)
    {
        $res = ImConfig::where('key', $key)
            ->where('type', ImConfig::THIRD_PARTY_TYPE)->first();
        return $res;
    }

    /**
     * 以key=>value形式获取配置
     * @param $data
     * @param array $res
     */
    private static function valueProcess($data, &$res = [])
    {
        foreach ($data as $val) {
            if (is_numeric($val['value'])) {
                $val['value'] = floatval($val['value']);
            }
            $res[$val['key']] = $val['value'];
        }
    }

    /**
     * 参数附加时间
     * @param $data array 二维数组参数
     * @param $type integer 1服务端 2客户端
     * @return array
     */
    private static function paramProcess($data, $type)
    {
        foreach ($data as &$val) {
            $val['type'] = $type;
            $val['created_at'] = time();
            $val['updated_at'] = time();
        }
        return $data;
    }

    /**
     * 返回随机字符串
     * @return mixed
     */
    public static function nickname()
    {
        $arr = ['菊花簪画闲', '明月的清寂', '花落校服无别事', '格式式最美', '细解语君难见', '森林枕无别事', '有一颗和娇妹', '不堪回首清颜野马', '敬你淮南归', '三生三世式惊鸿', '你是光芒旳小乔流水', '笑渐把酒戏天涯°', '每逢清酒花夕拾', '一纸解语偏无休', '花有花葬最美'];
        shuffle($arr);
        return $arr[0];
    }
}
